package com.suoluo.zenadmin.common.encrypt;

import java.security.MessageDigest;
import java.util.Random;

public class EncryptUtil {
	
	// 全局数组
    private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
    private static Random randGen = null;
	private static char[] numbersAndLetters = null;
    // 返回形式为数字跟字符�?
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        // System.out.println("iRet="+iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }

    /**
     * 产生随机字符�?
     * @param length
     * @return
     */
	public static final String randomString(int length) {
	         if (length < 1) {
	             return null;
	         }
	         if (randGen == null) {
	                randGen = new Random();
	                numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
	                   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                  //numbersAndLetters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                 }
	         char [] randBuffer = new char[length];
	         for (int i=0; i<randBuffer.length; i++) {
	             randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
	          //randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
	         }
	         return new String(randBuffer);
	}

    // 转换字节数组�?6进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }
    
	public static String md5(String data) {
		try {
			// 生成�?��MD5加密计算摘要
			MessageDigest md = MessageDigest.getInstance("md5"); // 同样可以使用SHA1
			// 计算md5函数
			md.update(data.getBytes());
			// digest()�?��确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就�?位的字符
			// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash�?
			// 参数也可不只�?6可改�?当然结果也不�?���?
			byte [] dataAry = md.digest();
//			System.out.println(Arrays.toString(dataAry));
//			String pwd = new BigInteger(1, dataAry).toString(16); 
			return byteToString(dataAry);
		} catch (Exception e) {
			throw new RuntimeException("fail to encrypt",e);
		}
	}
	
	public static String sha1(String data){
		try {
			// 生成�?��MD5加密计算摘要
			MessageDigest md = MessageDigest.getInstance("SHA-1"); // 同样可以使用SHA1
			// 计算md5函数
			md.update(data.getBytes());
			// digest()�?��确定返回sha1 hash值，返回值为8为字符串。因为sha1 hash值是16位的hex值，实际上就�?位的字符
			// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash�?
			// 参数也可不只�?6可改�?当然结果也不�?���?
//			String pwd = new BigInteger(1, md.digest()).toString(16); 
			byte [] dataAry = md.digest();
//			System.out.println(Arrays.toString(dataAry));
//			String pwd = new BigInteger(1, dataAry).toString(16); 
			return byteToString(dataAry);
		} catch (Exception e) {
			throw new RuntimeException("fail to encrypt",e);
		}
	}
	
//	public static void main(String []args){
//		//System.out.println("md5加密串："+EncryptUtil.md5("yjcsxdl1234"));
//		System.out.println("md5加密串："+EncryptUtil.md5("wyfdj1234"));
//		System.out.println("-----");
//		//System.out.println(MD5.encrypt("yjcsxdl1234"));
//		System.out.println(EncryptUtil.sha1("wyfdj1234"));
//	}
}
